/**
 * OWASP GoatDroid Project
 * 
 * This file is part of the Open Web Application Security Project (OWASP)
 * GoatDroid project. For details, please see
 * https://www.owasp.org/index.php/Projects/OWASP_GoatDroid_Project
 *
 * Copyright (c) 2011 - The OWASP Foundation
 * 
 * GoatDroid is published by OWASP under the GPLv3 license. You should read and accept the
 * LICENSE before you use, modify, and/or redistribute this software.
 * 
 * @author Jack Mannino, nVisium Security (https://www.nvisiumsecurity.com)
 * @created 2011
 */
package org.owasp.goatdroid.gui.webservices;

import java.io.File;

import javax.swing.JTree;
import org.owasp.goatdroid.gui.LessonTreeBuilder;
import org.owasp.goatdroid.jetty.webservicelauncher.*;

public class WebServices {

	static WebServiceLauncher service;
	static String slash;

	public WebServices(JTree jTree) {

		String serviceName = getServiceName(jTree);
		service = new WebServiceLauncher(serviceName);

	}

	static public String getServiceName(JTree jTree) {
		String slash;
		if (System.getProperty("os.name").toLowerCase().startsWith("windows"))
			slash = File.separator + File.separator;
		else
			slash = File.separator;
		LessonTreeBuilder lessonTree = new LessonTreeBuilder();
		System.out.println(slash + "before the madness tree "
				+ lessonTree.getCurrentNodeParent(jTree));

		if (lessonTree.getCurrentNodeParent(jTree).contains(File.separator)) {
			@SuppressWarnings("static-access")
			String[] directories = lessonTree.getCurrentNodeParent(jTree)
					.split(slash);
			// offset of our desired value
			System.out.println("tree: "
					+ lessonTree.getCurrentNodeParent(jTree));
			System.out.println("directories length " + directories.length);
			return directories[directories.length - 2].toLowerCase();
		} else {
			String[] directories = lessonTree.getCurrentNodeParent(jTree)
					.split(File.separator);
			System.out.println("more madness tree "
					+ lessonTree.getCurrentNodeParent(jTree));
			return lessonTree.getCurrentNodeParent(jTree);
		}

	}

	static public void startService() throws Exception {

		service.startService();
	}

	static public void stopService() throws Exception {
		service.stopService();

	}
}
